import { ErrorLocalCode } from '../defines/error.local.define';
import { IPlatform } from '../interface/platform.interface';
var Fly = require('flyio/dist/npm/ap');
var flyio = new Fly();
/*
 * @Author: wuzhouhai wzh200x@126.com
 * @Date: 2024-03-28 16:24:38
 * @LastEditors: wuzhouhai wzh200x@126.com
 * @LastEditTime: 2024-05-17 15:19:32
 * @FilePath: \phoenix_client_sdk\platform\platform.alipay.ts
 * @Description:
 */
export class PlatformAlipay implements IPlatform {
    public setBaseUrl(url: string) {
        flyio.config.baseURL = url;
        flyio.config.timeout = 5000;
        flyio.interceptors.request.use((config) => {
            config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
            return config;
        });
    }

    login(cb: Function): Promise<number | string | boolean> {
        return new Promise((resolve) => {
            let authCode = false;
            window['my'].getAuthCode({
                scopes: 'auth_user',
                success(res) {
                    if (res.authCode) {
                        //发起网络请求
                        console.log('alipay login back');
                        console.log(res);
                        authCode = res.authCode;
                        cb(authCode);
                        resolve(0);
                    }
                },
                fail(res) {
                    console.log('alipay login back fail');
                    console.log(res);
                    cb('');
                    resolve(ErrorLocalCode.PLATFORM_LOGIN_FAIED);
                },
            });
        });
    }

    public async request(method, url, data, token?): Promise<any> {
        return new Promise((resolve, reject) => {
            if (token) {
                flyio
                    .request(url, data, {
                        method: method,
                        headers: {
                            Authorization: `Bearer ${token}`,
                        },
                    })
                    .then((res) => {
                        console.log('request ok:', res);
                        resolve(res.data);
                    })
                    .catch((err) => {
                        console.log('err.message:', err);
                        if (err.response) {
                            reject(err.response.data);
                        } else {
                            reject({ code: ErrorLocalCode.REQUEST_FAILED, message: err.message });
                        }
                    });
            } else {
                flyio
                    .request(url, data, {
                        method: method,
                    })
                    .then((res) => {
                        console.log('request ok:', res);
                        resolve(res.data);
                    })
                    .catch((err) => {
                        if (err.response) {
                            reject(err.response.data);
                        } else {
                            console.log('err.message:', err);
                            reject({ code: ErrorLocalCode.REQUEST_FAILED, message: err.message });
                        }
                    });
            }
        });
    }
}
